# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import re
import tensorflow as tf
from . import smdp_gradients

class AdamWeightDecayOptimizer(tf.train.Optimizer):
  """A basic Adam optimizer that includes "correct" L2 weight decay."""

  def __init__(self,
               learning_rate,
               weight_decay_rate=0.0,
               beta_1=0.9,
               beta_2=0.999, 
               epsilon=1e-6,
               exclude_from_weight_decay=None,
               name="AdamWeightDecayOptimizer"):
    """Constructs a AdamWeightDecayOptimizer."""
    super(AdamWeightDecayOptimizer, self).__init__(False, name)

    self.learning_rate = learning_rate
    self.weight_decay_rate = weight_decay_rate
    self.beta_1 = beta_1
    self.beta_2 = beta_2
    self.epsilon = epsilon
    self.exclude_from_weight_decay = exclude_from_weight_decay


  def compute_gradients(self, loss, *args, **kwargs):
    grads_and_vars = smdp_gradients.compute_gradients( loss, recompute=True, var_partition=False )
    return grads_and_vars




  def apply_gradients(self, grads_and_vars, global_step=None, name=None):
    """See base class."""
    assignments = []
    global_step = tf.train.get_global_step()
    increase_global_step = tf.assign_add( global_step, tf.constant(1, dtype=tf.int64))

    for i, (grad, param) in enumerate(grads_and_vars):
      if grad is None or param is None:
        continue

      param_name = self._get_variable_name(param.name)

      m = tf.get_variable(
          name=param_name + "/adam_m",
          shape=param.shape.as_list(),
          dtype=tf.float32,
          trainable=False,
          initializer=tf.zeros_initializer())
      v = tf.get_variable(
          name=param_name + "/adam_v",
          shape=param.shape.as_list(),
          dtype=tf.float32,
          trainable=False,
          initializer=tf.zeros_initializer())

      # Standard Adam update.
      beta_1_grad = tf.multiply(1.0 - self.beta_1, grad)
      beta_2_grad = tf.multiply(1.0 - self.beta_2, tf.square(grad))
      beta_grad = tf.group(beta_1_grad, beta_2_grad)

      with tf.control_dependencies([beta_grad]):
          next_m = tf.multiply(self.beta_1, m) + beta_1_grad
          next_v = tf.multiply(self.beta_2, v) + beta_2_grad
 
     # next_m = (
     #     tf.multiply(self.beta_1, m) + tf.multiply(1.0 - self.beta_1, grad))
     # next_v = (
     #     tf.multiply(self.beta_2, v) + tf.multiply(1.0 - self.beta_2,
     #                                               tf.square(grad)))

      update = next_m / (tf.sqrt(next_v) + self.epsilon)

      # Just adding the square of the weights to the loss function is *not*
      # the correct way of using L2 regularization/weight decay with Adam,
      # since that will interact with the m and v parameters in strange ways.
      #
      # Instead we want ot decay the weights in a manner that doesn't interact
      # with the m/v parameters. This is equivalent to adding the square
      # of the weights to the loss with plain (non-momentum) SGD.
      with tf.control_dependencies([update]):
          if self._do_use_weight_decay(param_name):
              update = update + self.weight_decay_rate * param
          update_with_lr = self.learning_rate * update 
              
      # if i ==0:
      #   update = tf.Print(update,[update],message='!!!!!!!!!!!!!!!!!!!!!!!!!')

      #larm modified
      # larm = tf.norm(param)/tf.norm(update)
      # update_with_lr = self.learning_rate * larm * update

      #original
      #update_with_lr = self.learning_rate * update

      next_param = param - update_with_lr

      assignments.extend(
          [param.assign(next_param),
           m.assign(next_m),
           v.assign(next_v)])
    return tf.group(*assignments, increase_global_step)

  def _do_use_weight_decay(self, param_name):
    """Whether to use L2 weight decay for `param_name`."""
    if not self.weight_decay_rate:
      return False
    if self.exclude_from_weight_decay:
      for r in self.exclude_from_weight_decay:
        if re.search(r, param_name) is not None:
          return False
    return True

  def _get_variable_name(self, param_name):
    """Get the variable name from the tensor name."""
    m = re.match("^(.*):\\d+$", param_name)
    if m is not None:
      param_name = m.group(1)
    return param_name
